This page has two parts. The first part demonstrates a number of different ways to implement an "application server" as a CGI process. The second part shows some different ways to implement CGI scripts.
We will implement the CGI adder application using several different techniques.
Here is the source code to each of the CGI adder processes used in this page.
Use specific URL's and HTTP GET to call the CGI adderGET.exe
process.
cgi-bin/adderGET.exe?x=101&y=203 cgi-bin/adderGET.exe?101&203
Use a HTML form and HTTP GET to call the CGI adderGET.exe
process.
Use a HTML form and HTTP POST to call the CGI adderPOST.exe
process.
Use a HTML form, JavaScript, and either POST or GET to call the CGI adderPOST.exe
or adderGET.exe
processes.
Use a HTML form, Ajax, and HTTP GET to call the CGI adderAjaxGET.exe
process.
Use a HTML form, Ajax, and HTTP POST to call the CGI adderAjaxPOST.exe
process.
The next few examples are of "CGI scripts", rather than "CGI processes". The difference between a "CGI process" and a "CGI script" is subtle.
In the case of a "CGI process", the server creates the associated process as a child process and that child process does the required work for the HTTP client. The server communicates to the child process (using inter-process communication) the HTTP client's parameters.
In the case of a "CGI script" (e.g., a PHP script, a Python script, a CMD or Bash shell script, a Lua script, a Perl script, etc.) there is still a child process that is created by the web server. But in this case the child process is the needed interpreter for the associated scripting language. So, for example, to run a PHP CGI script, the web server runs the PHP interpreter as a child process and tells the interpreter (using inter-process communication) which PHP script to interpret. The server then communicates to the running script (again using inter-process communication) the HTTP client's parameters.
If you look inside of the source code to each of the CGI scripts, you will see that their first line is always a shebang (or hashbang) line. The Mongoose web server uses this line to find out which program (exe file) is the interpreter for that script. It is important to realize that the web server does not execute the CGI script directly. The web server must execute the appropriate interpreter process, and that interpreter process will "run the CGI script".
However, there are web servers that can run some scripting languages directly. In fact, the Mongoose web server can directly execute Lua scripts (the Lua interpreter is built into the Mongoose server). As another example, the Apache web server is sometimes set up to run PHP scripts as CGI scripts, and it is sometimes set up to execute PHP scripts directly.
Here is a simple Windows CMD CGI shell script. The shell scripts prints out all of the environment variables used by CGI processes, so this is a pretty interesting example of a CGI program. (Be sure to read the source code to this script.)
Here are two simple PHP CGI scripts (be sure to look at the source code to these script).
Try the PHP version of the adder application server.
Use a HTML form and HTTP GET to call the PHP CGI adder.php
script.
Try the Python version of the adder application server.
(You must have a version of Python installed on your computer and you might need to edit the shebang line in the adder.py
source file.)
Use a HTML form and HTTP GET to call the Python CGI adder.py
script.
Try the "Lua Server Page" version of an application server.
(This does not work with the "tiny" version of Mongoose.)
Go back to the static web page.